﻿#pragma once
enum Colour
{
	RED,
	BLACK
};
// 这⾥我们默认按key/value结构实现
template<class T>
struct RBTreeNode
{
	// 这⾥更新控制平衡也要加⼊parent指针
	T _data;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;
	RBTreeNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{
	}
};

template<class K, class T,class KeyofT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
	bool Insert(const T& data) {
		if (_root == nullptr) {
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur) {
			if (cur->_data < data) {
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_data > data) {
				parent = cur;
				cur = cur->_left;
			}
			else {
				return false;
			}
		}

		cur = new Node(data);
		cur->_col = RED;
		if (parent->_kv.first < kv.first) {
			parent->_right = cur;
		}
		else if (parent->_kv.first > kv.first) {
			parent->_left = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED) {
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent) {
				Node* uncle = grandfather->_right;
				if (uncle&& uncle->_col == RED) {
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					//继续往上
					cur = grandfather;
					parent = cur->_parent;

				}
				else{//叔叔不存在或者存在且为黑
					//    g
					//  p   u
					//c
					//右单选
					if (cur == parent->_left) {
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else {
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else {
				Node* uncle = grandfather->_left;
				// 叔叔存在且为红，-》变⾊即可
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					// 继续往上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else // 叔叔不存在，或者存在且为⿊
				{
					// 情况⼆：叔叔不存在或者存在且为⿊
					// 旋转+变⾊
					// g
					// u p
					// c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						// g
						// u p
						// c
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}
	void InOrder() {
		_InOrder(_root);
	}
	bool IsBalance() {
		if (_root == nullptr) {
			return true;
		}
		if (_root->_col == RED) {
			return false;
		}
		//
		Node* leftMost = _root;
		int c = 0;
		while (leftMost) {
			if (leftMost->_col == BLACK)++c;
			leftMost = leftMost->_left;
		}
		return Check(_root,0,c);
	}
private:
	bool Check(Node* cur,int blackNum,int c) {
		if (cur == nullptr) {
			if (blackNum != c) {
				cout << "黑色不相等" << endl;
				return false;
			}
			return true;
		}
		if (cur->_col == RED && cur->_parent && cur->_parent->_col == RED) {
			cout << cur->_kv.first << "->连续红色节点" << endl;
			return false;
		}
		if (cur->_col == BLACK) {
			blackNum++;
		}
		return Check(cur->_left,blackNum,c) && Check(cur->_right, blackNum,c);
	}
	void RotateR(Node* parent) {
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* parentP = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root) {
			_root = subL;
			subL->_parent = nullptr;
		}
		else {
			subL->_parent = parentP;

			if (parentP->_left == parent) {
				parentP->_left = subL;
			}
			else {
				parentP->_right = subL;
			}
		}
	}

	void RotateL(Node* parent) {
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentP = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (parent == _root) {
			_root = subR;
			subR->_parent = nullptr;
		}
		else {
			subR->_parent = parentP;

			if (parentP->_left == parent) {
				parentP->_left = subR;
			}
			else {
				parentP->_right = subR;
			}
		}
	}
	void _InOrder(Node* root) {
		if (root == nullptr) {
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}
Node* _root = nullptr;
};